#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
int n;

int dis(int x, int y) {
	return min({ x,y,n - x + 1,n - y + 1 });
}

void solve() {
	int x1, y1, x2, y2;
	cin >> n >> x1 >> y1 >> x2 >> y2;
	int t1 = dis(x1,y1), t2 = dis(x2,y2);
	cout << abs(t1 - t2) << endl;
}

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int t; cin >> t;
	while (t--) {
		solve();
	}
	return 0;
}